//
//  MyChart.m
//  SrnTeacher
//
//  Created by iDeneb on 5/15/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MyChart.h"
#import "db.h"
#import "SrnTeacherAppDelegate.h"
#import "Stat.h"

@implementation P_Point
@synthesize x,y;
+(id)makeWithX:(int)x Y:(int)y{
    P_Point *p=[[P_Point alloc] init];
    p.x=x; p.y=y;
    return p;
}
@end

@implementation MyChart 
@synthesize data;
@synthesize stats;
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
        //self.backgroundColor=[UIColor whiteColor];
        self.data=[[NSMutableArray alloc] init];
        //316>=p.x>=20
        //260>=p.y>=20
        [data addObject:[P_Point makeWithX:40 Y:130]];
        [data addObject:[P_Point makeWithX:50 Y:40]];
        [data addObject:[P_Point makeWithX:30 Y:20]];
        [data addObject:[P_Point makeWithX:300 Y:50]];
        [data addObject:[P_Point makeWithX:198 Y:220]];
        [data addObject:[P_Point makeWithX:124 Y:111]];
    } 
    return self;
}

- (void)drawRect:(CGRect)rect {
	self.data=[[NSMutableArray alloc] init];
	//[data addObject:[P_Point makeWithX:95 Y:80]];
	//[data addObject:[P_Point makeWithX:150 Y:40]];
	//[data addObject:[P_Point makeWithX:205 Y:70]];
	//[data addObject:[P_Point makeWithX:260 Y:120]];
	//[data addObject:[P_Point makeWithX:40 Y:160]];
	//[data addObject:[P_Point makeWithX:124 Y:100]];	
	
	db *udb = [SrnTeacherAppDelegate App].udb;
	int currSid = [SrnTeacherAppDelegate App].currid;
	NSMutableArray *temps = [udb getStatByNum:currSid];
	self.stats =temps;
	int len = [stats count];
	int total = 0;
	for (int j=0; j<len; j++) {
		Stat *s = [stats objectAtIndex:j];
		total+= s.count;
	}
	
    CGContextRef context=UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] setFill];
    NSString *title=[NSString stringWithFormat:@"Votes Cast:"];
    [title drawAtPoint:CGPointMake(120.0,0.0) withFont:[UIFont systemFontOfSize:25.0]];
    NSString *tstr=[NSString stringWithFormat:@"%d", total];
	[tstr drawInRect:CGRectMake(250,0,40,30) withFont:[UIFont systemFontOfSize:25.0]];
	
	
	CGContextSetRGBStrokeColor(context, 0.0, 0.6, 0.0, 1.0);
	CGContextSetLineWidth(context, 45.0);
	
	for (int j=0; j<len; j++) {
		Stat *s = [stats objectAtIndex:j];
		int t = s.count;
		NSString *choice = s.choice;
		NSString *cstr=[NSString stringWithFormat:@"%d", t];
		int p = (j+1)*70;
		float part = (float) t / (float) total;
		int y = 210 - (int) (part*210) + 90;
		UIImage *image;
		if([choice localizedCompare:@"yes"] == 0){
			image = [[UIImage imageNamed:@"yes.png"] retain];
		} else if([choice localizedCompare:@"no"] == 0){
			image = [[UIImage imageNamed:@"no.png"] retain];
		} else if([choice localizedCompare:@"a"] == 0){
			image = [[UIImage imageNamed:@"A.png"] retain];
		} else if([choice localizedCompare:@"b"] == 0){
			image = [[UIImage imageNamed:@"B.png"] retain];
		} else if([choice localizedCompare:@"c"] == 0){
			image = [[UIImage imageNamed:@"C.png"] retain];
		} else if([choice localizedCompare:@"d"] == 0){
			image = [[UIImage imageNamed:@"D.png"] retain];
		} else if([choice localizedCompare:@"e"] == 0){
			image = [[UIImage imageNamed:@"E.png"] retain];
		} else if([choice localizedCompare:@"hand"] == 0){
			image = [[UIImage imageNamed:@"raise.png"] retain];
		} else if([choice localizedCompare:@"maybe"] == 0){
			image = [[UIImage imageNamed:@"maybe.png"] retain];
		} else {
			image = [[UIImage imageNamed:@"maybe.png"] retain];
		}
		CGRect rect;
		rect = CGRectMake(25, p-25, 60, 60);
		//UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
		//imageView.image = image;
		[image drawInRect:rect];
		//[self addSubview:imageView];
		//[imageView release];
		//[choice drawInRect:CGRectMake(40, p,40,30) withFont:[UIFont systemFontOfSize:15.0]];	
		CGContextMoveToPoint(context, 300, p);
        CGContextAddLineToPoint(context, y, p);
		CGContextStrokePath(context);
		float pertage = part * 100;
		NSString *tstr = [NSString stringWithFormat:@"%@%@%1.2f%@", cstr, @"/", pertage, @"%"];
		[tstr drawInRect:CGRectMake(200,p-15,100,100) withFont:[UIFont systemFontOfSize:20.0]]; 
	}
	
	
	/*
    // Drawing lines with a Black stroke color
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
    // Draw them with a 1.0 stroke width so they are a bit more visible.
    CGRect rc=CGRectMake(20.0, 20.0, rect.size.width-31, rect.size.height-81);
    CGContextStrokeRectWithWidth(context, rc,1.0);
    
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
    CGContextSetLineWidth(context, 0.3);
    //画刻度线
	if (total < 10) {
		total = 10;
	} else if (total < 20) {
		total = 20;
	} else if (total < 30) {
		total = 30;
	} else if (total < 50) {
		total = 50;
	} else if (total < 100) {
		total = 100;
	}
			

	
    for(int i=1;i<=10;i++){
        CGContextMoveToPoint(context, 20.0, 20.0+24.0*i);
        CGContextAddLineToPoint(context, rect.size.width-10, 20.0+24.0*i);
        CGContextStrokePath(context);
        int tv = total - i * total/10;
        NSString *value=[NSString stringWithFormat:@"%d", tv];
        [value drawAtPoint:CGPointMake(3.0,10.0+24.0*i) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
    }
	
	NSString *max = [NSString stringWithFormat:@"%d", total];
    [max drawAtPoint:CGPointMake(3.0,10) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
    //[@"0" drawAtPoint:CGPointMake(3.0,250) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
    
    //画刻度线
   // NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
   // [dateFormat setDateFormat: @"MM-dd\nHH时"]; 
    
   // NSDate *offsetDate=[[NSDate date] addTimeInterval:-24*3*3600];
   // NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
   // NSDateComponents *offsetComponents = [gregorian components:NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit fromDate:offsetDate];
   // [offsetComponents setHour:12];
    // Calculate when, according to Tom Lehrer, World War III will end
   // NSDate *startDate = [gregorian dateFromComponents:offsetComponents];
   // [gregorian release];
    for(int j=0;j<len;j++){
        //NSString *time=[dateFormat stringFromDate: startDate];
		Stat *s = [stats objectAtIndex:j];
		NSString *choice = s.choice;
		int t = s.count;
		NSString *sc=[NSString stringWithFormat:@"%d", t];		
        [choice drawInRect:CGRectMake(40+55.0*j,260.0,40,30) withFont:[UIFont systemFontOfSize:11.0]];
		[sc drawInRect:CGRectMake(40+55.0*j,270.0,40,30) withFont:[UIFont systemFontOfSize:11.0]];        //startDate=[startDate addTimeInterval:12*3600];
    }
    CGContextSetLineWidth(context, 2.0);
    for(int k=0;k<len-1;k++){
        CGContextMoveToPoint(context, 70.0+55*k, 260.0);
        CGContextAddLineToPoint(context, 70.0+55*k, 255);
        CGContextStrokePath(context);
    }
    //config the style of lines
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
    CGContextSetLineWidth(context, 20.0);
    //draw the chart of data
	int x= 40;
	for (int j=0; j<len; j++) {
		if (j == 0) {
			x = 45;
		} else {
		    x = 40 + 55 *j;
		}
		Stat *s = [stats objectAtIndex:j];
		int t = s.count;
		float part = (float) t / (float) total;
		int y = 240 - (int) (part*240) + 20;
		[data addObject:[P_Point makeWithX:x Y:y]];
	}	
    for(int i=0;i<[data count];i++){
        P_Point *p=[data objectAtIndex:i];
        CGContextMoveToPoint(context, p.x, 260.0);
        CGContextAddLineToPoint(context, p.x, p.y);
        CGContextStrokePath(context);
    }
	 */
}
- (void)dealloc {
    [super dealloc];
	[stats release];
	[data release];
}
@end
